<?php
namespace Yd\Controller;
use Common\Controller\HomebaseController;

class CreditController extends HomebaseController{
	
	protected $reservation_model;
	function _initialize() {
		parent::_initialize();
		$this->model = D("users");
	}
	
	/*预订*/
	function index(){
		$v_credit = $this->get_credit();
		$v_credit_need = $this->credit_need();
		$v_submit_button = $this->show_button($v_credit,$v_credit_need);
		$this->assign("v_credit", $v_credit);
		$this->assign("v_credit_need", $v_credit_need);
		$this->assign("v_submit_button",$v_submit_button);
		$this->display();
	}
	
	function ajax_credit_reset(){
		if(!sp_check_verify_code()){
			$this->error("验证码错误！");
		}
		if (IS_POST) {
			$credit_reset = I('post.credit_reset');  
			if ($credit_reset != "on") {$this->error("请在\"同意自觉遵守乒羽中心管理规定\"上打勾！");}
			
			$reason = I('post.input_reason','','htmlspecialchars');
			$user_id = sp_get_current_userid();
			$notice_model = D("Common/Notice");
			$result = $notice_model->credit_reset($user_id,1,$reason);
			//$this->success("credit_reset:" . $credit_reset . "； reason:" . $reason . "； user_id:" . $user_id . "； result:" . $result);
			if ($result > 0) {
				//$to=empty($_SESSION['notice_index'])?"Yd/Notice/index":$_SESSION['notice_index'];
				$this->success("成功申请，请耐心等待管理员回复！");
			} else {
				$this->error("申请失败，您可以去乒羽中心现场向管理员申请！");
			}
		}
	}
	
	/*信用分增加操作，$number为要增加的分数*/
	function credit_setInc($user_id,$number){
		$where['id']=$user_id;
		$User->where($where)->setInc('credit',$number); // 信用分增加
		
	}
	/*用户信用分减少操作*/
	function credit_setDec($user_id,$number){
		$where['id']=$user_id;
		$User->where($where)->setDec('credit',$number); // 信用分减少
	}
	/*取出当前信用分要求*/
	function credit_need(){
		$sysset_model = D("Common/Sysset");
		$ret = $sysset_model->get_sysset_valueid("credittype");
		return $ret;
	}
	/*判断是否显示恢复信用分按钮*/
	function show_button($credit,$credit_need){
		$button = "<a id=\"modal-697846\" href=\"#modal-container-697846\" role=\"button\" class=\"btn btn-primary btn-lg\" data-toggle=\"modal\">申请恢复信用分</a>";
		if ($credit < $credit_need){
			return $button;
		}
		return '';
	}
	/*取信用分*/
	function get_credit(){
		$user_id = sp_get_current_userid();
		$model = D("Common/Sysset");
		$ret = $model->get_credit($user_id);
		return $ret;
	}

}
